﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.IO;
namespace QUANLYDIEMHOCSINH
{
    public partial class QUANLYTAIKHOAN : Form
    {
        TAIKHOANBUS tkbus = new TAIKHOANBUS();
        public QUANLYTAIKHOAN()
        {
            InitializeComponent();
            set_AN();
            LoadDanhSachTaiKhoan();
        }
        void set_AN()
        {
            if (File.Exists("AnhNen.txt"))
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string s = null;
                string t = null;
                while ((s = sr.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("Turquoise");
                sw.Flush();
                sw.Close();
                fs.Close();

                FileStream fs1 = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr1 = new StreamReader(fs1);
                string s = null;
                string t = null;
                while ((s = sr1.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs1.Close();
            }
        }
        void LoadDanhSachTaiKhoan()
        {
            dtgrDanhSachTaiKhoan.DataSource = tkbus.DanhSachTaiKhoan();
            dtgrDanhSachTaiKhoan.Columns[2].Visible = false;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            THEMTAIKHOAN themtk = new THEMTAIKHOAN();
            themtk.KhiThemMoi += frm_ThemTaiKhoan;
            themtk.ShowDialog();
        }
        void frm_ThemTaiKhoan()
        {
            LoadDanhSachTaiKhoan();
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (tentaikhoan == "")
            {
                MessageBox.Show("Chưa Chọn Tài Khoản Cần Xóa!");
                return;
            }
            CAPNHATTAIKHOAN capnhattk = new CAPNHATTAIKHOAN();
            capnhattk.ShowDialog();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if(tentaikhoan=="")
            {
                MessageBox.Show("Chưa Chọn Tài Khoản Cần Xóa!");
                return;
            }
            if(tkbus.XoaTaiKhoan(tentaikhoan)==1)
            {
                MessageBox.Show("Xóa Thành Công!");
                tentaikhoan = "";
                LoadDanhSachTaiKhoan();
            }
            else
            {
                MessageBox.Show("Xóa Thất Bại!");
            }
        }

        public static string tentaikhoan="";
        private void dtgrDanhSachTaiKhoan_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                tentaikhoan = dtgrDanhSachTaiKhoan.Rows[e.RowIndex].Cells[1].Value.ToString();
            }
        }

        private void dtgrDanhSachTaiKhoan_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dtgrDanhSachTaiKhoan.Rows[e.RowIndex].Cells["stt"].Value = e.RowIndex + 1;
        }
    }
}
